package com.qf.base.controller;

import com.mchange.v1.util.ArrayUtils;
import com.qf.base.pojo.UserVO;
import com.qf.base.utils.JWTUtil;
import org.apache.poi.util.ArrayUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaseController {
    public Long userId(){
    return user()==null?null:user().getId();

    }
    public UserVO user(){
//        获取servlet请求属性对象
        ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//      根据请求属性对象获取请求对象
        HttpServletRequest request = servletRequestAttributes.getRequest();
//        根据请求对象获取请求头的token信息
        String token = request.getHeader("token");
        return JWTUtil.checkToken(token);
    }
    public void flushToken(){
//        获取servlet请求属性对象
        ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//      根据请求属性对象获取请求对象
        HttpServletRequest request = servletRequestAttributes.getRequest();
//        根据请求属性对象发送请求对象
        HttpServletResponse response = servletRequestAttributes.getResponse();
        String oldToken = request.getHeader("token");
        response.setHeader("new-token", JWTUtil.createTokenByOldToken(oldToken));
    }
}
